package me.xiaoyan.android.widget;

import me.xiaoyan.android.tuition.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class YanDialog extends Dialog {
	private Context context;
	private String title;
	private String message;
	private String positiveButtonText;
	private String negativeButtonText;
	private DialogInterface.OnClickListener positiveButtonClickListener,
			negativeButtonClickListener;

	private TextView m_title;
	private TextView m_message;
	private Button m_btn1;
	private Button m_btn2;

	public YanDialog(Context context) {
		super(context, R.style.yanDialog);
		setContentView(R.layout.alert);

		// find controls
		m_title = (TextView) findViewById(R.id.alertTitle);
		m_message = (TextView) findViewById(R.id.alertMessage);
		m_btn1 = (Button) findViewById(R.id.alertButton1);
		m_btn2 = (Button) findViewById(R.id.alertButton2);
	}

	public YanDialog setDTitle(String title) {
		this.title = title;
		return this;
	}

	public YanDialog setDTitle(int title) {
		this.title = (String) context.getText(title);
		return this;
	}
	
	public YanDialog setMessage(String message) {
		this.message = message;
		return this;
	}

	public YanDialog setMessage(int message) {
		this.message = (String) context.getText(message);
		return this;
	}

	/**
	 * Set the positive button resource and it's listener
	 * 
	 * @param positiveButtonText
	 * @param listener
	 * @return
	 */
	public YanDialog setPositiveButton(int positiveButtonText,
			DialogInterface.OnClickListener listener) {
		this.positiveButtonText = (String) context.getText(positiveButtonText);
		this.positiveButtonClickListener = listener;
		return this;
	}

	/**
	 * Set the positive button resource and it's listener
	 * 
	 * @param positiveButtonText
	 * @param listener
	 * @return
	 */
	public YanDialog setPositiveButton(String positiveButtonText,
			DialogInterface.OnClickListener listener) {
		this.positiveButtonText = positiveButtonText;
		this.positiveButtonClickListener = listener;
		return this;
	}

	/**
	 * Set the negative button resource and it's listener
	 * 
	 * @param positiveButtonText
	 * @param listener
	 * @return
	 */
	public YanDialog setNegativeButton(String negativeButtonText,
			DialogInterface.OnClickListener listener) {
		this.negativeButtonText = negativeButtonText;
		this.negativeButtonClickListener = listener;
		return this;
	}

	/**
	 * Set the negative button resource and it's listener
	 * 
	 * @param positiveButtonText
	 * @param listener
	 * @return
	 */
	public YanDialog setNegativeButton(int negativeButtonText,
			DialogInterface.OnClickListener listener) {
		this.negativeButtonText = (String) context.getText(negativeButtonText);
		this.negativeButtonClickListener = listener;
		return this;
	}

	@Override
	public void show() {
		if (title != null) {
			m_title.setText(title);
		}
		if (message != null) {
			m_message.setText(message);
		}
		
		if (positiveButtonText != null) {
			m_btn1.setText(positiveButtonText);
			m_btn1.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					positiveButtonClickListener.onClick(YanDialog.this,
							DialogInterface.BUTTON_POSITIVE);
				}
			});
		} else {
			m_btn1.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					YanDialog.this.cancel();
				}
			});
		}
		
		if (negativeButtonText != null) {
			m_btn2.setText(negativeButtonText);
			m_btn2.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					negativeButtonClickListener.onClick(YanDialog.this,
							DialogInterface.BUTTON_NEGATIVE);
				}
			});
		} else {
			m_btn2.setVisibility(View.GONE);
		}
		super.show();
	}
}
